package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.WeatherData;
import com.tbit.main.pojo.WeatherDate;
import com.tbit.main.pojo.Weatherhistory;
import com.tbit.main.service.AccountWeatherService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 天气统计
 *
 * @Author: chen
 * @Description:
 * @Date: 创建于 15:38 2023/3/6
 */
@RestController
@RequestMapping("/accountWeather")
public class AccountWeatherController {

    @Autowired
    private AccountWeatherService accountWeatherService;
    @Autowired
    private TokenService tokenService;

    /**
     * 分时获取天气详情
     *
     * @param weatherData
     * @return
     */
    @RequestMapping(value = "/getHoursWeather")
    public JsonResult getHoursWeather(WeatherData weatherData, String token) {
        if (weatherData.getAccountId() == null) {
            AccountUser accountUser = tokenService.getAccount(token);
            weatherData.setAccountId(accountUser.getAccountId());
        }

        List<Weatherhistory> weatherhistories = accountWeatherService.getHoursWeather(weatherData.getAccountId(), weatherData.getStartTime(), weatherData.getEndTime());
        return JsonResult.succ(weatherhistories);
    }

    /**
     * 按天获取天气详情
     *
     * @param accountId 区域id
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @RequestMapping("/getDaysWeather")
    public JsonResult getDaysWeather(Integer accountId, String startTime, String endTime, String token) {
        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        if (accountId == null) {
            AccountUser accountUser = tokenService.getAccount(token);
            accountId = accountUser.getAccountId();
        }

        List<WeatherDate> weatherDates = accountWeatherService.getDaysWeather(accountId, startTime, endTime);
        return JsonResult.succ(weatherDates);
    }

    /**
     * 周线获取天气分布
     *
     * @param accountId 区域id
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @RequestMapping("/groupDaysWeather")
    public JsonResult groupDaysWeather(Integer accountId, String startTime, String endTime, String token) {
        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        if (accountId == null) {
            AccountUser accountUser = tokenService.getAccount(token);
            accountId = accountUser.getAccountId();
        }
        List<Map<String, String>> result = accountWeatherService.groupByTime(accountId, startTime, endTime);

        return JsonResult.succ(result);
    }

    /**
     * 月线获取天气分布
     *
     * @param accountId 区域id
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param token
     * @return
     */
    @RequestMapping("/groupMonthsWeather")
    public JsonResult groupMonthsWeather(Integer accountId, String startTime, String endTime, String token) {
        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        if (accountId == null) {
            AccountUser accountUser = tokenService.getAccount(token);
            accountId = accountUser.getAccountId();
        }
        List<Map<String, String>> result = accountWeatherService.groupByMonth(accountId, startTime, endTime);

        return JsonResult.succ(result);
    }


}
